﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Threading;
using System.Windows.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.ChaineHotel
{
    /// <summary>
    /// Interaction logic for P_ChaineHotel.xaml
    /// </summary>
    public partial class P_ChaineHotel : Page
    {
        HotelClient srvService;
        Chaine_Hotels ch;

        public P_ChaineHotel()
        {
            InitializeComponent();
            ch = new Chaine_Hotels();
        }

        private void bt_ajouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //ch = new Chaine_Hotels();

                ch.Nom_Chzine_Hotel = tb_NomChaineHotel.Text.Trim();
                srvService = new HotelClient();
                srvService.Open();
                int resultatAjout = srvService.AddChaineHotels(ch);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("La chaine d'hotel [" + ch.Nom_Chzine_Hotel + "] est bien ajoutée",
                                    "Ajout Chaine Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeChaineHotel();
                }
                else
                {
                    MessageBox.Show("Erreur d'ajout de la chaine d'hotel [" + ch.Nom_Chzine_Hotel + "]",
                                    "Ajout Chaine Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Chaine Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// Charge de manière asynchrone la liste des hotels
        /// </summary>
        private void loadListeChaineHotel()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des chaînes d'hotel en cours...");
                    srvService = new HotelClient();
                    srvService.Open();
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_ChaineHotel.DataContext = srvService.getListChaineHotels();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Chaine d'hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void gererListeChaineHotel(object sender, RoutedEventArgs e)
        {
            try
            {
                Button bt = (Button)sender;
                Chaine_Hotels uneChaine = ((Button)sender).DataContext as Chaine_Hotels;

                string nameBT = (string)bt.Name;
                if (nameBT == "affHotelButton")
                {
                    dg_hotel.DataContext = uneChaine.Hotels;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void editMenu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuItem mn = (MenuItem)sender;
                Chaine_Hotels uneChaine = ((MenuItem)sender).DataContext as Chaine_Hotels;

                string nameBT = (string)mn.Name;
                //if (nameBT == "affHotelButton")
                //{
                //    dg_hotel.DataContext = uneChaine.Hotels;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void deleteMenu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuItem mn = (MenuItem)sender;
                Chaine_Hotels uneChaine = ((MenuItem)sender).DataContext as Chaine_Hotels;
                if (MessageBox.Show("Voulez-vous supprimer l'hotel", "Suppression", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    srvService = new HotelClient();
                    srvService.Open();
                    //ch = uneChaine;
                    int resultatDel = srvService.deleteChaineHotels(uneChaine);
                    loadListeChaineHotel();
                    srvService.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void dg_ChaineHotel_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var a = sender;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;
            loadListeChaineHotel();
        }

        private void dg_hotel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var element = e.MouseDevice.DirectlyOver;
                if (element != null && element is FrameworkElement)
                {
                    var grid = sender as DataGrid;

                    if (((FrameworkElement)element).Parent is DataGridCell)
                    {
                        if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                        {
                            var _hotel = grid.SelectedItem as Hotels;
                            W_Hotel.pageEncours = new ui.Hotelss.P_Hotel(_hotel);
                            W_Hotel.framePage.Navigate(W_Hotel.pageEncours);

                        }
                    }
                    else
                    {
                        if (grid != null) grid.SelectedItem = null;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
